#include "statusbaritem.h"

StatusBarItem::StatusBarItem() :
     d(new StatusBarItemPrivate)
 {
     d->offset = 0;

     setPen(QPen(QBrush(), 0));
     setBrush(QBrush(QColor(0,0,0,120)));

     d->textItem = new QGraphicsSimpleTextItem(this);
     d->textItem->setBrush(QBrush(Qt::white));

     setText("");
 }

StatusBarItem::~StatusBarItem() {
    delete d;
}

void StatusBarItem::setText(QString text)
 {
     d->textItem->setText(text);
     QRectF rect = d->textItem->boundingRect();
     QPointF delta = this->rect().center() - rect.center();
     d->textItem->setPos(0, delta.y());
 }

void StatusBarItem::setRect(qreal x, qreal y, qreal w, qreal h)
{
    QGraphicsRectItem::setRect(x, y + d->offset, w, h);
    setText(d->textItem->text());
}

void StatusBarItem::setOffset(int offset)
{
    this->setY(this->y() - d->offset + offset);
    d->offset = offset;
}

void StatusBarItem::show()
{
    QPropertyAnimation *anim = new QPropertyAnimation(this, "offset");
    anim->setStartValue(0);
    anim->setEndValue(-1 * rect().height());
    anim->setDuration(500);
    anim->start(QAbstractAnimation::DeleteWhenStopped);
}

void StatusBarItem::hide()
{
    QPropertyAnimation *anim = new QPropertyAnimation(this, "offset");
    anim->setStartValue(d->offset);
    anim->setEndValue(0);
    anim->setDuration(500);
    anim->start(QAbstractAnimation::DeleteWhenStopped);
}

void StatusBarItem::showText(QString text, quint32 timeout)
{
    setText(text);
    show();
    QTimer::singleShot(timeout, this, SLOT(hide()));
}
